clear;clc;

R_0     =   load('Result_Alternative_0.mat');
R_1     =   load('Result_Alternative_4.mat');
R_2     =   load('Result_Alternative_5.mat');
R_3     =   load('Result_Alternative_6.mat');

R_List      =   {R_0,R_1,R_2,R_3};
T_IRF       =   13;
List_Shock  =   {'Eps_Eta_M_dom','Eps_Eta_M_ext','Eps_Eta_Y_H'};

Folder      =   'TableGraph//Portfolio//';
mkdir(Folder);
%% Aggregate Variables
%--------------------------------------------------------------------------
% Setup
%--------------------------------------------------------------------------
% Graphics
N_col       =   1;
N_row       =   1;
fig_size    =   [N_col,N_row].*[1,0.75]*1/2.5; 
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.15,0.02];
fig_HMargin =   [0.18,0.02]; 

fig_SubPlot =   @(ii_vv)subtightplot(N_row,N_col,ii_vv,...
                                     fig_gap,fig_VMargin,fig_HMargin);
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',8,'interpreter','latex','location','best');
XLabel      =   @(Label)xlabel(Label,'fontsize',8,'interpreter','latex');
YLabel      =   @(Label)ylabel(Label,'fontsize',8,'interpreter','latex');

AxisSetup   =   GR_Axis('TickFmt',struct('X','%g','Y','%.2g'),...
                        'FontSize',struct('X',8,'Y',8),...
                        'Exponent',struct('X',0,'Y',0),...
                        'Box','off');

Tick_X      =   @(X)GR_TickValue('Unit',4).Setup(X);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0).Setup(Y);
%--------------------------------------------------------------------------
% Different Figures
%--------------------------------------------------------------------------
% Prices
LabelList   =   {'$\phi=1000$','$\phi=0.1$','$\phi=0.05$','$\phi=0.02$'};

TempFun_LP  =   @(S,C)GR_Line('Style',S,'Color',MyColor(C),'Width',3);
List_LP     =   {TempFun_LP('-','Black');TempFun_LP('--','Gray');...
                 TempFun_LP(':','Blue'); TempFun_LP('-.','Red')};

for ii_Irf=1:length(List_Shock)
    SH          =   List_Shock{ii_Irf};
    IRF_s_pos   =   zeros(length(R_List),T_IRF);
    IRF_s_neg   =   zeros(length(R_List),T_IRF);
    IRF_r_pos   =   zeros(length(R_List),T_IRF);
    IRF_r_neg   =   zeros(length(R_List),T_IRF);
    for jj=1:length(R_List)
        RR          =   R_List{jj};
        r_dom       =   RR.MODEL.VAR.ir_dom.Dev2Level(RR.IRF.(SH).ir_dom);
        r_ext       =   RR.MODEL.VAR.ir_ext.Dev2Level(RR.IRF.(SH).ir_ext);
        s_pos       =   RR.MODEL.VAR.DomShare_Pos.Dev2Level(RR.IRF.(SH).DomShare_Pos);
        s_neg       =   RR.MODEL.VAR.DomShare_Neg.Dev2Level(RR.IRF.(SH).DomShare_Neg);
        AdjCost     =   PortfolioAdjCost('AdjCost',RR.PP.Fin_AdjCost, ...
                                     'Share_Target',RR.PP.Fin_DomShare,...
                                     'r_dom',r_dom,'r_ext',r_ext);
        EffRet_pos  =   AdjCost.EffectiveReturn(1,s_pos);
        EffRet_neg  =   AdjCost.EffectiveReturn(-1,s_neg);
        
        IRF_s_pos(jj,:)=RR.IRF.(SH).DomShare_Pos(1:T_IRF)'*100;
        IRF_s_neg(jj,:)=RR.IRF.(SH).DomShare_Neg(1:T_IRF)'*100;
        IRF_r_pos(jj,:)=(EffRet_pos(1:T_IRF)'-EffRet_pos(end))*100*4;
        IRF_r_neg(jj,:)=(EffRet_neg(1:T_IRF)'-EffRet_neg(end))*100*4;
    end
    
    Fig         =   FigureSetup(['Domestic Share of Saver to ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    
    [PH,ax]     =   Plot_IRF(IRF_s_pos,'ax',ax,'LineProperty',List_LP,...
                                        'XTick',Tick_X((0:T_IRF-1)'),'YTick',Tick_Y(IRF_s_pos),...
                                        'AxisProperty',AxisSetup);
    XLabel('Quarter');
    YLabel('$\%$');
    LEG(PH,LabelList);   

    print('-depsc','-r1000',Fig,[Folder,'Share_Saver_',SH]);
    close(Fig);
    
    Fig         =   FigureSetup(['Domestic Share of Borrower to ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    
    [PH,ax]     =   Plot_IRF(IRF_s_neg,'ax',ax,'LineProperty',List_LP,...
                                        'XTick',Tick_X((0:T_IRF-1)'),'YTick',Tick_Y(IRF_s_neg),...
                                        'AxisProperty',AxisSetup);
    XLabel('Quarter');
    YLabel('$\%$');
    LEG(PH,LabelList);   

    print('-depsc','-r1000',Fig,[Folder,'Share_Borrower_',SH]);
    close(Fig);
    
    Fig         =   FigureSetup(['Effective Return of Saver to ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    
    [PH,ax]     =   Plot_IRF(IRF_r_pos,'ax',ax,'LineProperty',List_LP,...
                                        'XTick',Tick_X((1:T_IRF)'-1),'YTick',Tick_Y(IRF_r_pos),...
                                        'AxisProperty',AxisSetup);
    XLabel('Quarter');
    YLabel('$\%$');
    LEG(PH,LabelList);   

    print('-depsc','-r1000',Fig,[Folder,'Return_Saver_',SH]);
    close(Fig);
    
    Fig         =   FigureSetup(['Effective Return of Borrower to ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    
    [PH,ax]     =   Plot_IRF(IRF_r_neg,'ax',ax,'LineProperty',List_LP,...
                                        'XTick',Tick_X((1:T_IRF)'-1),'YTick',Tick_Y(IRF_r_neg),...
                                        'AxisProperty',AxisSetup);
    XLabel('Quarter');
    YLabel('$\%$');
    LEG(PH,LabelList);   

    print('-depsc','-r1000',Fig,[Folder,'Return_Borrower_',SH]);
    close(Fig);
end